import dayjs from 'dayjs'

// 模拟角色列表
export const roles = [
  { value: 'admin', label: '系统管理员' },
  { value: 'operator', label: '运营人员' },
  { value: 'content', label: '内容管理员' },
  { value: 'viewer', label: '访客' }
]

// 模拟用户列表
export const users = [
  {
    id: '1',
    username: 'admin',
    name: '系统管理员',
    password: '123456',
    avatar: 'https://tdesign.gtimg.com/site/avatar.jpg',
    email: 'admin@example.com',
    phone: '13800000001',
    role: 'admin',
    status: 'active',
    permissions: ['*'],
    createTime: '2023-01-01 00:00:00',
    lastLoginTime: '2024-06-07 12:30:00',
    department: '技术部'
  },
  {
    id: '2',
    username: 'operator',
    name: '张运营',
    password: '123456',
    avatar: 'https://tdesign.gtimg.com/site/avatar.jpg',
    email: 'operator@example.com',
    phone: '13800000002',
    role: 'operator',
    status: 'active',
    permissions: ['content:*', 'activity:*'],
    createTime: '2023-02-15 00:00:00',
    lastLoginTime: '2024-06-06 09:15:00',
    department: '运营部'
  },
  {
    id: '3',
    username: 'content1',
    name: '李文案',
    password: '123456',
    avatar: 'https://tdesign.gtimg.com/site/avatar.jpg',
    email: 'content1@example.com',
    phone: '13800000003',
    role: 'content',
    status: 'active',
    permissions: ['content:view', 'content:edit'],
    createTime: '2023-03-20 00:00:00',
    lastLoginTime: '2024-06-05 14:45:00',
    department: '内容部'
  },
  {
    id: '4',
    username: 'content2',
    name: '王编辑',
    password: '123456',
    avatar: 'https://tdesign.gtimg.com/site/avatar.jpg',
    email: 'content2@example.com',
    phone: '13800000004',
    role: 'content',
    status: 'inactive',
    permissions: ['content:view', 'content:edit'],
    createTime: '2023-04-05 00:00:00',
    lastLoginTime: '2024-05-20 11:20:00',
    department: '内容部'
  },
  {
    id: '5',
    username: 'viewer1',
    name: '赵访客',
    password: '123456',
    avatar: 'https://tdesign.gtimg.com/site/avatar.jpg',
    email: 'viewer1@example.com',
    phone: '13800000005',
    role: 'viewer',
    status: 'locked',
    permissions: ['content:view'],
    createTime: '2023-05-10 00:00:00',
    lastLoginTime: '2024-04-18 16:30:00',
    department: '市场部'
  }
]

// 根据条件筛选用户
export function filterUsers(params: any) {
  let filteredUsers = [...users]
  
  // 关键词搜索
  if (params.keyword) {
    const keyword = params.keyword.toLowerCase()
    filteredUsers = filteredUsers.filter(
      user => user.username.toLowerCase().includes(keyword) || 
              user.name.toLowerCase().includes(keyword) ||
              user.email?.toLowerCase().includes(keyword) ||
              user.phone?.includes(keyword)
    )
  }
  
  // 角色筛选
  if (params.role) {
    filteredUsers = filteredUsers.filter(user => user.role === params.role)
  }
  
  // 状态筛选
  if (params.status) {
    filteredUsers = filteredUsers.filter(user => user.status === params.status)
  }
  
  // 计算总数
  const total = filteredUsers.length
  
  // 分页
  const page = Number(params.page) || 1
  const pageSize = Number(params.pageSize) || 10
  const start = (page - 1) * pageSize
  const end = start + pageSize
  
  // 排序 - 默认按创建时间降序
  filteredUsers.sort((a, b) => {
    return dayjs(b.createTime).valueOf() - dayjs(a.createTime).valueOf()
  })
  
  // 提取分页数据
  const pageData = filteredUsers.slice(start, end)
  
  return {
    list: pageData,
    total,
    page,
    pageSize
  }
}

// 根据ID获取用户
export function getUserById(id: string) {
  return users.find(user => user.id === id)
} 